// stores/timer.js
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export const useTimerStore = defineStore('timer', () => {
    const countdownTime = ref(15 * 60); // 15分钟
    const timer = ref(null);

    const minutes = computed(() => String(Math.floor(countdownTime.value / 60)).padStart(2, '0'));
    const seconds = computed(() => String(countdownTime.value % 60).padStart(2, '0'));

    const startCountdown = () => {
        clearInterval(timer.value); // 清除之前的计时器
        countdownTime.value = 15 * 60; // 重置倒计时
        timer.value = setInterval(() => {
            if (countdownTime.value > 0) {
                countdownTime.value--;
            } else {
                clearInterval(timer.value);
                // 倒计时结束逻辑
                console.log("倒计时结束，支付超时");
            }
        }, 1000);
    };

    const stopCountdown = () => {
        clearInterval(timer.value);
    };

    return {
        countdownTime,
        minutes,
        seconds,
        startCountdown,
        stopCountdown
    };
});
